package utils

import (
	"math/rand"
	"strings"
	"time"
)

// InArrayFloat64  浮点数数组是否含有该元素 str 要检查是否存在的元素 arr要检查的数组
// 返回值 存在返回true 不存在返回false
func InArrayFloat64(str float64, arr []float64) (isExist bool) {
	for _, v := range arr {
		if v == str {
			isExist = true
			break
		}
	}
	return
}

func InArrayFloat32(f float32, arr []float32) (isExist bool) {
	for _, v := range arr {
		if v == f {
			isExist = true
			break
		}
	}
	return
}

// InArrayInt 整数数组是否存在该元素  str 要检查是否存在的元素 arr要检查的数组
// 返回值 存在返回true 不存在返回false
func InArrayInt(str int, arr []int) (isExist bool) {
	for _, v := range arr {
		if v == str {
			isExist = true
			break
		}
	}
	return
}

func InArrayInt8(i8 int8, arr []int8) (isExist bool) {
	for _, v := range arr {
		if v == i8 {
			isExist = true
			break
		}
	}
	return
}

func InArrayInt16(i16 int16, arr []int16) (isExist bool) {
	for _, v := range arr {
		if v == i16 {
			isExist = true
			break
		}
	}
	return
}

func InArrayInt32(i32 int32, arr []int32) (isExist bool) {
	for _, v := range arr {
		if v == i32 {
			isExist = true
			break
		}
	}
	return
}

func InArrayInt64(i64 int64, arr []int64) (isExist bool) {
	for _, v := range arr {
		if v == i64 {
			isExist = true
			break
		}
	}
	return
}

func InArrayByte(b byte, arr []byte) (isExist bool) {
	for _, v := range arr {
		if v == b {
			isExist = true
			break
		}
	}
	return
}

// InArrayString 字符串数组是否存在该元素 str 要检查是否存在的元素 arr要检查的数组
// 返回值 存在返回true 不存在返回false
func InArrayString(str string, arr []string) (isExist bool) {
	for _, v := range arr {
		if v == str {
			isExist = true
			break
		}
	}
	return
}

// ArrayDiffInt 比较两个int型数组是否相等，如果相等返回的数组为nil，否则返回两者的唯一合集
func ArrayDiffInt(arr1, arr2 []int) []int {
	var diffArr []int
	for _, v := range arr1 {
		if !InArrayInt(v, arr2) {
			diffArr = append(diffArr, v)
		}
	}
	for _, v := range arr2 {
		if !InArrayInt(v, arr1) {
			diffArr = append(diffArr, v)
		}
	}
	return diffArr
}

// ArrayDiffInt32 返回两个数组的非交集
func ArrayDiffInt32(arr1, arr2 []int32) []int32 {
	var diffArr []int32
	for _, v := range arr1 {
		if !InArrayInt32(v, arr2) {
			diffArr = append(diffArr, v)
		}
	}
	for _, v := range arr2 {
		if !InArrayInt32(v, arr1) {
			diffArr = append(diffArr, v)
		}
	}
	return diffArr
}

// 比较两个string型数组是否相等，如果相等返回的数组为nil，否则返回两者的唯一合集
func ArrayDiffString(arr1, arr2 []string) []string {
	var diffArr []string
	for _, v := range arr1 {
		if !InArrayString(v, arr2) {
			diffArr = append(diffArr, v)
		}
	}
	for _, v := range arr2 {
		if !InArrayString(v, arr1) {
			diffArr = append(diffArr, v)
		}
	}
	return diffArr
}

// IsArrarIntExistRepeat 数组是否存在重复元素
func IsArrarIntExistRepeat(arr []int) bool {
	m := make(map[int]bool)
	for _, v := range arr {
		if m[v] {
			return true
		}
		m[v] = true
	}
	return false
}

// IsArrarInt64ExistRepeat 数组是否存在重复元素
func IsArrarInt64ExistRepeat(arr []int64) bool {
	m := make(map[int64]bool)
	for _, v := range arr {
		if m[v] {
			return true
		}
		m[v] = true
	}
	return false
}

// IsArrarInt32ExistRepeat 数组是否存在重复元素
func IsArrarInt32ExistRepeat(arr []int32) bool {
	m := make(map[int32]bool)
	for _, v := range arr {
		if m[v] {
			return true
		}
		m[v] = true
	}
	return false
}

// ArrayI32ToStringSlice int32数组转换成string数组
func ArrayI32ToStringSlice(arr []int32) []string {
	strs := make([]string, 0, len(arr))
	for _, v := range arr {
		strs = append(strs, I32toA(v))
	}
	return strs
}

// ArrayI32Join int32数组拼接成string
func ArrayI32Join(arr []int32, symbol string) string {
	if len(arr) == 0 {
		return ""
	}

	if len(arr) == 1 {
		return I32toA(arr[0])
	}
	var strArr []string

	for _, v := range arr {
		strArr = append(strArr, I32toA(v))
	}
	return strings.Join(strArr, symbol)
}

// ArrayI64Join int64数组拼接成string
func ArrayI64Join(arr []int64, symbol string) string {
	if len(arr) == 0 {
		return ""
	}

	if len(arr) == 1 {
		return I64toA(arr[0])
	}
	var strArr []string

	for _, v := range arr {
		strArr = append(strArr, I64toA(v))
	}
	return strings.Join(strArr, symbol)
}

// StrSplitToI32Arr
func StrSplitToI32Arr(str string, symbol string) []int32 {
	if str == "" {
		return []int32{}
	}

	arr := strings.Split(str, symbol)
	var rs = []int32{}

	for _, v := range arr {
		if v == "" {
			continue
		}
		rs = append(rs, AtoI32(v))
	}

	return rs
}

// StrSplitToI64Arr
func StrSplitToI64Arr(str string, symbol string) []int64 {
	if str == "" {
		return []int64{}
	}

	arr := strings.Split(str, symbol)
	var rs = []int64{}

	for _, v := range arr {
		if v == "" {
			continue
		}
		rs = append(rs, AtoI64(v))
	}

	return rs
}

// ShuffleArrI32 打乱数组
func ShuffleArrI32(SliceInfo []int32) []int32 {
	if len(SliceInfo) == 0 {
		return SliceInfo
	}
	randomNum := int64(rand.Intn(maxNum) + minNum)
	rand.Seed(time.Now().UnixNano() + randomNum)
	//打乱切片顺序
	rand.Shuffle(len(SliceInfo), func(i, j int) {
		SliceInfo[i], SliceInfo[j] = SliceInfo[j], SliceInfo[i]
	})
	return SliceInfo
}

// StrArrJoin
func StrArrJoin(s []string, symbol string) string {
	return strings.Join(s, symbol)
}
